package com.cuitrip.util;

import android.app.Application;
import android.location.Location;
import android.os.Bundle;
import android.os.RemoteException;
import android.text.TextUtils;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.amap.api.maps2d.MapsInitializer;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;

/**
 * Created by MiJack on 2015/11/9.
 */
public class MapUtil {

    private static MapController instance;

    public static  boolean debugGoogleMap(){
        return  false;
    }
    public synchronized static MapController getDefaultInstance() {
        if (instance == null) {
            if (!gmsAvailable){
                instance =new GaoDeMapController();
            } else{
                instance = (debugGoogleMap()||useGoogleMap) ? new GoogleMapController() : new GaoDeMapController();
            }
        }
        return instance;
    }

    public static void init(Application application) {
        try {
            MapsInitializer.initialize(application);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        int googlePlayServicesAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(application);
        if (googlePlayServicesAvailable != ConnectionResult.SUCCESS){
            gmsAvailable=false;
            return;
        }
        gmsAvailable=true;
        //获取位置信息
        LocationManagerProxy mLocationManagerProxy = LocationManagerProxy.getInstance(application);
        //此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
        //注意设置合适的定位时间的间隔，并且在合适时间调用removeUpdates()方法来取消定位请求
        //在定位结束后，在合适的生命周期调用destroy()方法
        //其中如果间隔时间为-1，则定位只定一次
        mLocationManagerProxy.requestLocationData(
                LocationProviderProxy.AMapNetwork, -1, 1000, listener);
        mLocationManagerProxy.setGpsEnable(false);
    }

    private static boolean useGoogleMap = false;
    private static  boolean gmsAvailable=false;

    public static boolean isGmsAvailable() {
        return gmsAvailable;
    }

    private static AMapLocationListener listener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation amapLocation) {
            if (amapLocation != null) {
                if (amapLocation.getCountry() != null && amapLocation.getCountry().equals("中国")) {
                    useGoogleMap = false;
                    if (amapLocation.getProvince() != null && amapLocation.getProvince().contains("台湾") || amapLocation.getProvince().contains("香港")
                            || amapLocation.getProvince().contains("澳门")) {
                        useGoogleMap = true;
                    }
                } else {
                    if (!TextUtils.isEmpty(amapLocation.getCountry())){
                        useGoogleMap = true;
                    }else{
                        useGoogleMap=false;
                    }
                }
            }
        }

        @Override
        public void onLocationChanged(Location location) {

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    };

}
